package com.itheima.jwt;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;

public class JwtTest {
    public static void main(String[] args) {
        //获取系统时间
        long timeMillis = System.currentTimeMillis();
        Date date = new Date(timeMillis);

        //生成jwt令牌
        JwtBuilder builder = Jwts.builder()
                .setId("66")//设置jwt编码
                .setSubject("黑马程序员")//设置jwt主题
                .setIssuedAt(new Date())//设置jwt签发时间
                //.setExpiration(date)//设置jwt过期时间
                .claim("roles","admin")
                .claim("company","itheima")
                .signWith(SignatureAlgorithm.HS256,"itheima");

        //生成jwt
        String compact = builder.compact();
        System.out.println(compact);

        //解析jwt，得到其内部的数据
        Claims claims = Jwts.parser().setSigningKey("itheima").parseClaimsJws(compact).getBody();
        System.out.println(claims);
    }
}
